package com.lepu.app.fun.index.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemLongClickListener;

import com.app.utils.ObserverConst;
import com.app.utils.UmengEvent;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.Observer;
import com.core.lib.utils.ObserverManager;
import com.core.lib.utils.main.UmengHelper;
import com.lepu.app.fun.index.adapter.IndexRemindAdapter;
import com.lepu.app.fun.index.remind.Alarm;
import com.lepu.app.fun.index.remind.AlarmTipsManager;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;
import com.lepu.pasm.R;

public class IndexRemindActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, OnTopbarNewRightButtonListener, Observer {
	
    private ListView mDataListView = null;
	private IndexRemindAdapter mDataAdapter = null;
	private ArrayList<Alarm> mDataList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		mNeedDefaultUmeng = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.index_remind_activity);

		init();
	}
	
    @Override
	public void onResume() {
        super.onResume();

        UmengHelper.onResume(this, UmengEvent.page_shouye_yongyaotixing);
    }

    @Override
	public void onPause() {
        super.onPause();

        UmengHelper.onPause(this, UmengEvent.page_shouye_yongyaotixing);
    }

	@Override
	public void onDestroy() {
		ObserverManager.getInstance().removeObserver(ObserverConst.NOTIFY_REMIND_CHANGE);
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}
	
	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	@Override
	public void onTopbarRightButtonSelected() {
		Intent intent = new Intent(this, IndexRemindAddActivity.class);
		startActivity(intent,true);
	}

	private void showHideNoDataLayout(boolean isShow)
	{
		LinearLayout noDataLayout = (LinearLayout) findViewById(R.id.noDataLayout);
		if(noDataLayout != null)
		{
			if(isShow)
			{
				noDataLayout.setVisibility(View.VISIBLE);
				mDataListView.setVisibility(View.GONE);
			}else{
				noDataLayout.setVisibility(View.GONE);
				mDataListView.setVisibility(View.VISIBLE);
			}
		}
	}
	
	/**
	 * 初始化方法
	 */
	private void init() {
		ObserverManager.getInstance().addObserver(ObserverConst.NOTIFY_REMIND_CHANGE, this);
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("用药提醒");
		topbar.setRightText("添加");
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setOnTopbarNewRightButtonListener(this);
		
		mDataListView = (ListView) findViewById(R.id.dataListView);
		
		mDataList = AlarmTipsManager.getAlarmByGroupName();
		if (mDataList == null || mDataList.size() == 0) {
			showHideNoDataLayout(true);
		} else {
			mDataAdapter = new IndexRemindAdapter(IndexRemindActivity.this,mDataList);
			mDataListView.setAdapter(mDataAdapter);
			showHideNoDataLayout(false);
		}
		
		
		//长按删除
		mDataListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				
//				Alarm item = mDataList.get(position);
//				deleteRemind(item.yaoName);
				return false;//default false
			}
		});
	}
	


	//刷新列表
	@Override
	public void notify(String arg0, Object arg1, Object arg2) {
		mDataList = AlarmTipsManager.getAlarmByGroupName();
		
		if (mDataList == null || mDataList.size() == 0) {
			showHideNoDataLayout(true);
		} else {
			mDataAdapter = new IndexRemindAdapter(IndexRemindActivity.this,mDataList);
			mDataListView.setAdapter(mDataAdapter);
			showHideNoDataLayout(false);
		}
	}

}
